import { createApp } from 'vue'
import App from './App.vue'
import axios from 'axios'
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css'

import zh from "element-plus/dist/locale/zh-cn.mjs";
import en from "element-plus/dist/locale/en.mjs";
import ru from "element-plus/dist/locale/ru.min.mjs";



const localeMapping = {
    zh: zh,
    en: en,
    ru: ru,
};

// import locale from 'element-plus/lib/locale/lang/zh-cn';
// import './assets/yahei.css'
import router, { state } from './router';

import TranslationService from './api/TranslationService'


const app = createApp(App)

// 配置axios
// axios.defaults.baseURL = 'http://localhost:5000/api'
//编译时必须用下面这个
// axios.defaults.baseURL = 'http://172.16.14.99:5000/api'
axios.defaults.baseURL = 'http://127.0.0.1:5000/api'
    // axios.defaults.baseURL = 'http://192.167.0.20:5000/api'
    // axios.defaults.baseURL = 'http://172.20.10.2:5000/api'
    // axios.defaults.baseURL = 'http://192.167.8.104:5000/api'

// 根据运行环境设置baseURL
// const isWindows = process.platform === 'win32'
// axios.defaults.baseURL = isWindows ?
//     'http://172.16.14.99:5000/api' // Windows下使用指定IP
//     :
//     'http://127.0.0.1:5000/api' // 其他环境使用127.0.0.1


// 将axios注入到Vue的原型中
app.config.globalProperties.$axios = axios
app.config.globalProperties.$test = "666"

// 将 state 对象注入全局
app.config.globalProperties.$state = state


// 获取URL中的lang参数
function getLangFromUrl() {
    const queryString = window.location.search
    const urlParams = new URLSearchParams(queryString)
    return urlParams.get('lang')
}

var locale = localeMapping[getLangFromUrl() || 'zh']

// console.log(locale)
app.use(ElementPlus, { 'locale': locale });

// app.config.globalProperties.$axios = axios

const translationService = new TranslationService()
app.provide('translationService', translationService)

translationService.loadTranslation().then(() => {
    app.config.globalProperties.$t = (text) => {
        const lang = state.lang || 'en'
            // switchLanguage(lang)
        return translationService.translate(text, lang)
    }

    app.use(router)
    app.mount('#app')

})

// axios.get('/translations').then(response => {
//     app.config.globalProperties.$t = (text) => {
//         const lang = state.lang || 'en'
//         const translationService = inject('translationService')
//         return translationService.translate(text, lang)
//     }
//     app.use(router)
//     app.mount('#app')
// })

// app.mount('#app')